function [x, y, u, v, vorticity, snr, is_valid, is_masked] = ...
    fluere_dat2mtx(filename)
% KYLE P. LYNCH - lynchkp@auburn.edu
% June 9, 2011
%
% fluere_dat2mtx.m
%   Converts Fluere data file output from .dat files into MATLAB matrices.
%  
%   Designed for Fluere 0.8.
%
%   Usage: [x, y, u, v, vorticity, snr1, validity, mask] =
%       fluere_dat2mtx(filename)
%
%   Inputs:
%   
%   FILENAME is the location of the .dat file to be processed.       
%
%   Outputs:
%
%   X is a 2-D array providing the horizontal coordinates of the vectors.
%   Y is a 2-D array providing the vertical coordinates of the vectors.
%   U is a 2-D array providing the horizontal component of vectors.
%   V is a 2-D array providing the vertical component of vectors.
%   VORTICITY is a 2-D array providing the vorticity.
%   SNR is a 2-D array providing the SNR values.
%   IS_VALID is a 2-D array providing the boolean vector validity flag.
%   IS_MASKED is a 2-D array providing the boolean vector mask flag.

if nargin < 1
    error('Not enough input arguments. Filename not specified.');
end

[x, y, u, v, vorticity, snr, is_valid, is_masked] = ...
    textread(filename, '%f %f %f %f %f %f %f %f', ...
    'delimiter', ',', 'headerlines', 3);

ni = numel(unique(x(:)));
nj = numel(unique(y(:)));

% keyboard;

x = reshape(x, ni, nj)';
y = reshape(y, ni, nj)';
u = reshape(u, ni, nj)';
v = reshape(v, ni, nj)';
vorticity = reshape(vorticity, ni, nj)';
snr = reshape(snr, ni, nj)';
is_valid = reshape(is_valid, ni, nj)';
is_masked = reshape(is_masked, ni, nj)';

end